package org.blogwu.learn.algorithm.链表学习;

public class _双向循环链表<E> {

    transient int size = 0;

    /**
     * head节点
     */
    transient Node<E> first;

    /**
     * 尾节点
     */
    transient Node<E> last;

    // 当前迭代节点
    transient Node<E> current;

    public E getCurrentItem() {
        return current.item;
    }

    public _双向循环链表() {
        Node<E> node = new Node<E>();
        node.prev = node;
        node.next = node;
        first = node;
        last = first;
    }

    public void add(E e) {
//        System.out.println(111);
        // 如果没元素
        if (size == 0) {
            first.setItem(e);
            size++;
        } else {
            Node<E> newNode = this.creatNew(e, last);
            newNode.next = first;
            this.last = newNode;
            this.first.prev = last;
            size++;
        }
    }

    /**
     * 重置指针
     */
    public void reset() {
        current = first;
    }

    /**
     * 获取下一个节点
     *
     * @return
     */
    public E getNext() {
        if (current == first) {
            System.out.println("到达首节点");
//            return null;
        }
        current = current.getNext();
        return current.getItem();
    }

    public E removeCurrent() {
        if (this.current == null) {
            System.out.println("removeCurrent null");
            return null;
        }
        E e = current.getItem();
        size--;
        if (this.current == first) {
            this.current = first.getNext();
            first = first.getNext();
            last.next = first;
            first.prev = last;
            return e;
        }
        if (this.current == last) {
            this.current = last.getNext();
            last = last.getPrev();
            first.prev = last;
            last.next = first;
            return e;
        }
        current.prev.next = current.next;
        current.next.prev = current.prev;
        current = current.next;
        return e;
    }

    private Node<E> creatNew(E e, Node<E> pre) {
        Node<E> node = new Node<E>(e);
        node.prev = pre;
        pre.next = node;
        return node;
    }

    public int size() {
        return size;
    }


    // 双向链表
    private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        public Node() {
        }

        public Node(E item) {
            this.item = item;
        }


        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }

        public E getItem() {
            return item;
        }

        public void setItem(E item) {
            this.item = item;
        }

        public Node<E> getNext() {
            return next;
        }

        public void setNext(Node<E> next) {
            this.next = next;
        }

        public Node<E> getPrev() {
            return prev;
        }

        public void setPrev(Node<E> prev) {
            this.prev = prev;
        }
    }
}
